home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2820 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  93 lines

  1. Path: ohstpy.mps.ohio-state.edu!vancleef
  2. From: vancleef@ohstpy.mps.ohio-state.edu
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf problems
  5. Message-ID: <1996Jan24.032124.8802@ohstpy>
  6. Date: 24 Jan 96 03:21:24 -0500
  7. References: <4e4c2v$j2g@mathserv.mps.ohio-state.edu> <4e4h0j$af7@fountain.mindlink.net>
  8. Organization: The Ohio State University, Department of Physics
  9.  
  10. In article <4e4h0j$af7@fountain.mindlink.net>, genew@mindlink.bc.ca (Gene Wirchenko) writes:
  11. > Chris Mongold <cmongold@magnus.acs.ohio-state.edu> wrote:
  12. >>Hello,
  13. >>    I'm sorry if this is an inappropriate topic, but I've tried
  14. >>everything else.  I can't seem to get sscanf to to separate a string
  15. >>into various variable types.  Here is an example:
  16. >>#include <stdio.h>
  17. >>void main()
  18. >    ^
  19. >      This is a no-no.  Per the Standard, main() returns int.
  20. >>{
  21. >>char input[20], crap[17], segment[6], seg_len[3], begin[3], load[3];
  22. >>int type;
  23. >>FILE *fp;
  24. >>printf("File: ");
  25. >>gets(input);
  26. >      Buffer can be overflowed.  You might want to use fgets().
  27. >>fp = fopen(input, "r");
  28. >      You don't check if the file could be opened.  This may be the
  29. > problem.  Add:
  30. >           if (fp==NULL)
  31. >              {
  32. >              printf("Input file could not be opened.\n");
  33. >              exit(EXIT_FAILURE);
  34. >              }
  35. >>while(!feof(fp))
  36. >>{
  37. >>fgets(crap, 17, fp);
  38. >>sscanf(crap, "%d%3s%6s%3s%3s", type, begin, segment, seg_len, load);
  39.  
  40.                                  ^^^^^  Try &type
  41.  
  42.  
  43. >>printf("%d %3s %6s %3s %3s", type, begin, segment, seg_len, load);
  44. >                                 ^
  45. >      type is never assigned a value.
  46.  
  47. He tries to do it above. sscanf requires the address of the target variable.
  48.  
  49. >>}
  50. >      Since main() returns a value, return one.  Add:
  51. >           return 0;
  52. >>}
  53. >>No matter what I do, it always 'bus errors' when I sscanf.  I've tried
  54. >>it several different ways, including making crap larger, but to
  55. >>no avail.  If anyone can help me, I would greatly appreciate it.
  56. >      I wonder if your data is in the wrong format.  Include a copy of
  57. > it.
  58. >      I suspect you'll need to copy the chars to their dest.
  59. >>Please respond to cmongold@magnus.acs.ohio-state.edu 
  60. >>Thanks,
  61. >>Chris 
  62. > Sincerely,
  63. > Gene Wirchenko
  64. > C Pronunciation Guide:
  65. >      y=x++;     "wye equals ex plus plus semicolon"
  66. >      x=x++;     "ex equals ex doublecross semicolon"
  67.  
  68. -Garrett
  69.  
  70.  
  71.  
  72.